﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OAuth
{
    //
    // Extensions to the String class
    //
    internal static class StringExtensions
    {
        //
        // Characters that aren't Url encoded
        //
        private static readonly char[] UnreservedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~".ToCharArray();

        //
        // Url encode a string
        //
        internal static string UrlEncode(this string s)
        {
            if (s == null)
            {
                throw new NullReferenceException();
            }

            var sb = new StringBuilder();
            foreach (var ch in s)
            {
                if (UnreservedChars.Contains(ch)) sb.Append(ch);
                else sb.Append(Uri.HexEscape(ch));
            }
            return sb.ToString();
        }
    }
}
